﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using YDWin.Base;
using YDServices;
using YDModel;
using DevExpress.Utils;
using YDCommon;
using static YDServices.Enum.OrderEnum;

namespace YDWin.Printing
{
    public partial class AddPrintingForm : AddForm
    {
        PrintingService svr = new PrintingService();
        FactoryService fs = new FactoryService();
        SpecService ss = new SpecService();
        SystemParameterService sps = new SystemParameterService();
        public T_Order_Details editTu = new T_Order_Details();
        public T_Order_Details editLi = new T_Order_Details();
        public string state = "";

        public AddPrintingForm()
        {
            InitializeComponent();


            //设置日期控件格式
            this.deOperationTime.Properties.DisplayFormat.FormatString = "yyyy年MM月dd日";
            this.deOperationTime.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.deOperationTime.Properties.EditFormat.FormatString = "yyyy年MM月dd日";
            this.deOperationTime.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.deOperationTime.Properties.Mask.EditMask = "yyyy年MM月dd日";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
       
            T_Order_Details fact = new T_Order_Details();
            if (!state.Equals("") && state.Equals("Edit"))
            {
                fact = editTu;
            }

            fact.Factory_ID = (long)cboFactoryName.EditValue;
            fact.Factory_Name = cboFactoryName.Text;
            fact.Spec_Name = txtSpecName.Text;
            fact.GateWidth = txtGateWidth.Text;
            fact.GramWeight = txtGramWeight.Text;
            fact.FlowerName = cboFlowerName.Text;
            fact.FlowerNumber = cboFlowerNumber.Text;

            fact.OperatintTime = (DateTime)deOperationTime.EditValue;
            fact.Quantity = decimal.Parse(nudQuantity.Text); 

            fact.StartWeight = decimal.Parse(nudStartWeight.Text); 
            fact.ProcessUnitPrice = decimal.Parse(nudProcessUnitPrice.Text);
            fact.ProcessCost = decimal.Parse(nudProcessCost.Text); 
 

            if (!state.Equals("") && state.Equals("Add") && editLi != null)//新增
            {
                fact.OrderDetails_ID = editLi.ID;
                fact.OrderType = (int)OrderTyp.印花;  //印花
                fact.SurplusQuantity = fact.Quantity;
                fact.UseQuantity = 0;
                fact.SurplusWeight = fact.StartWeight;
                fact.UseWeight = 0;
                fact.Weight = editLi.Weight ;
                fact.TotalCost = (editLi.TotalCost / editLi.Quantity) * fact.Quantity + fact.ProcessCost;//成本=上次的总价/数量 *当次数量 +当次工费
            }

            WaitDialogForm sdf = new WaitDialogForm("", "正在保存......");
            if (!state.Equals("") && state.Equals("Edit"))
            {
                 svr.EditItem(fact);
            }
            else
            {
                svr.AddItem(fact);
            } 
            sdf.Close();

            if (svr.HasError)
            {
                Utils.ShowPrompt(MessageType.mtError, "印花信息保存失败：" + svr.Errors);
                svr.Errors.Clear();
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void AddPrintingForm_Load(object sender, EventArgs e)
        {
            //厂家名称下拉框 印花
            var fDs = fs.getComboxList((int)OrderTyp.印花);
            cboFactoryName.Properties.ValueMember = "ID";   //实际要用的字段
            cboFactoryName.Properties.DisplayMember = "FactoryName";    //要显示的字段
            cboFactoryName.Properties.DataSource = fDs;
            cboFactoryName.Properties.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;//输入之后自动显示并选中下拉内容

            //花号
            var FNList = sps.getComboxList("FlowerName");
            cboFlowerName.Properties.ValueMember = "Value";   //实际要用的字段
            cboFlowerName.Properties.DisplayMember = "Name";    //要显示的字段
            cboFlowerName.Properties.DataSource = FNList;
            cboFlowerName.Properties.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;//输入之后自动显示并选中下拉内容

            //花色
            var FNBList = sps.getComboxList("FlowerNumber");
            cboFlowerNumber.Properties.ValueMember = "Value";   //实际要用的字段
            cboFlowerNumber.Properties.DisplayMember = "Name";    //要显示的字段
            cboFlowerNumber.Properties.DataSource = FNBList;
            cboFlowerNumber.Properties.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;//输入之后自动显示并选中下拉内容


            if (!state.Equals("") && state.Equals("Add") && editLi != null)//新增
            {
                deOperationTime.EditValue = DateTime.Now;
                nudQuantity.Text = (string.IsNullOrEmpty(editLi.SurplusQuantity.ToString()) ? "0" : editLi.SurplusQuantity.ToString());
                nudStartWeight.Text = (string.IsNullOrEmpty(editLi.SurplusWeight.ToString()) ? "0" : editLi.SurplusWeight.ToString());
                txtSpecName.Text = editLi.Spec_Name.ToString();
            }

            if (!state.Equals("") && state.Equals("Edit") && editTu != null)//编辑
            {
                cboFactoryName.Text = editTu.Factory_Name;
                txtSpecName.Text = editTu.Spec_Name;
                txtGateWidth.Text = editTu.GateWidth;
                txtGramWeight.Text = editTu.GramWeight;
                cboFlowerName.Text = editTu.FlowerName;
                cboFlowerNumber.Text = editTu.FlowerNumber;


                deOperationTime.EditValue = editTu.OperatintTime;
                nudQuantity.Text = editTu.Quantity.ToString();
                nudStartWeight.Text = editTu.StartWeight.ToString();
                nudProcessUnitPrice.Text = editTu.ProcessUnitPrice.ToString();
                nudProcessCost.Text = editTu.ProcessCost.ToString();  

                nudQuantity.Enabled = false;
                nudStartWeight.Enabled = false;

            }


        }


        #region 数量录入后自动计算 
        private void nudQuantity_KeyUp(object sender, KeyEventArgs e)
        {
            if (decimal.Parse(nudQuantity.Text) > editLi.Quantity)
            {
                nudQuantity.Text = editLi.SurplusQuantity.ToString();
                Utils.ShowPrompt(MessageType.mtInformation, "数量超过最大库存数量!");
            }
            nudStartWeight.Text = (editLi.Weight * decimal.Parse(nudQuantity.Text)).ToString();
        }

        private void nudStartWeight_KeyUp(object sender, KeyEventArgs e)
        {
            nudProcessCost.Text = (decimal.Parse(nudStartWeight.Text) * decimal.Parse(nudProcessUnitPrice.Text)).ToString();
        }

        private void nudProcessUnitPrice_KeyUp(object sender, KeyEventArgs e)
        {
            nudProcessCost.Text = (decimal.Parse(nudStartWeight.Text) * decimal.Parse(nudProcessUnitPrice.Text)).ToString();
        }





        #endregion
 
         
    }
}